import { createStore } from 'vuex';

const store = createStore({
  state() {
    return {
      userInfo: null,
      routerData: [],
    };
  },
  getters: {
    getUserInfo: state => {
      return state.userInfo;
    },
    getRouterData: state => {
      return state.routerData;
    },
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    setRouterData(state, data) {
      state.routerData = data;
    },
  },
  actions: {
    setUserInfo(context, userInfo) {
      context.commit('setUserInfo', userInfo);
    },
    setRouterData(context, routerData) {
      context.commit('setRouterData', routerData);
    },
  },
  plugins: [
    (() => {
      return store => {
        store.subscribe((mutation, state) => {
          localStorage.setItem('estoreConsole_vuex', JSON.stringify(state));
        });
      };
    })(),
  ],
});

export default store;
